package com.town.garden.service.impl;

import cn.hutool.json.JSONUtil;
import com.town.garden.constant.GlobalConstant;
import com.town.garden.entiy.Result;
import com.town.garden.entiy.request.FaceAddRequest;
import com.town.garden.entiy.request.FaceSearchRequest;
import com.town.garden.enums.NVRApiEnum;
import com.town.garden.propertites.NvrProperties;
import com.town.garden.service.NvrFaceService;
import com.town.garden.storage.NvrSessionStorage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Description
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/5/17
 */
@Service
@Slf4j
@AllArgsConstructor
public class NvrFaceServiceImpl implements NvrFaceService {
    private final RestTemplate restTemplate;
    private final NvrProperties nvrProperties;
    @Override
    public Result addFace(FaceAddRequest faceAddRequest) {
        final String url = GlobalConstant.HTTPS_PREFIX + nvrProperties.getNvrServerIp() + NVRApiEnum.FACES_ADD.getUrl();
        final HttpEntity<?> entity = new HttpEntity<>(JSONUtil.parse(faceAddRequest), NvrSessionStorage.headers);
        final ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity,String.class);
        if (HttpStatus.OK.value() == exchange.getStatusCodeValue()) {
            return Result.ok(JSONUtil.parseObj(exchange.getBody()));
        }
        return Result.fail();
    }

    /**
     * 查询名单 第一次查询会返回查询数量 放入到缓存 需要做第二次查询
     * @param faceAddRequest
     * @return
     */
    @Override
    public Result searchFace(FaceAddRequest faceAddRequest) {
        final String url = GlobalConstant.HTTPS_PREFIX + nvrProperties.getNvrServerIp() + NVRApiEnum.FACES_SEARCH.getUrl();
        final HttpEntity<?> entity = new HttpEntity<>(JSONUtil.parse(faceAddRequest), NvrSessionStorage.headers);
        final ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity,String.class);
        if (HttpStatus.OK.value() == exchange.getStatusCodeValue()) {
            return Result.ok(JSONUtil.parseObj(exchange.getBody()));
        }
        return Result.fail();
    }

    /**
     * 分页获取人脸图片库
     * @param faceSearchRequest
     * @return
     */
    @Override
    public Result getByIndex(FaceSearchRequest faceSearchRequest) {
        final String url = GlobalConstant.HTTPS_PREFIX + nvrProperties.getNvrServerIp() + NVRApiEnum.FACES_GETBYINDEX.getUrl();
        final HttpEntity<?> entity = new HttpEntity<>(JSONUtil.parse(faceSearchRequest), NvrSessionStorage.headers);
        final ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity,String.class);
        if (HttpStatus.OK.value() == exchange.getStatusCodeValue()) {
            return Result.ok(JSONUtil.parseObj(exchange.getBody()));
        }
        return Result.fail();
    }

    @Override
    public Result faceModify(FaceAddRequest faceAddRequest) {
        final String url = GlobalConstant.HTTPS_PREFIX + nvrProperties.getNvrServerIp() + NVRApiEnum.FACES_MODIFY.getUrl();
        final HttpEntity<?> entity = new HttpEntity<>(JSONUtil.parse(faceAddRequest), NvrSessionStorage.headers);
        final ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity,String.class);
        if (HttpStatus.OK.value() == exchange.getStatusCodeValue()) {
            return Result.ok(JSONUtil.parseObj(exchange.getBody()));
        }
        return Result.fail();
    }

    @Override
    public Result faceRemove(FaceAddRequest faceAddRequest) {
        final String url = GlobalConstant.HTTPS_PREFIX + nvrProperties.getNvrServerIp() + NVRApiEnum.FACES_REMOVE.getUrl();
        final HttpEntity<?> entity = new HttpEntity<>(JSONUtil.parse(faceAddRequest), NvrSessionStorage.headers);
        final ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity,String.class);
        if (HttpStatus.OK.value() == exchange.getStatusCodeValue()) {
            return Result.ok(JSONUtil.parseObj(exchange.getBody()));
        }
        return Result.fail();
    }
}
